"""版本一"""
from rest_framework.generics import GenericAPIView
from meiduo_admin.serializer.sku_serializer import *
from meiduo_admin.pages import MyPage
from rest_framework.response import Response
from goods.models import *


# 请求方式： GET /meiduo_admin/skus/?keyword=<名称|副标题>&page=<页码>&page_size=<页容量>
class SKUViewSet(GenericAPIView):
    queryset = SKU.objects.all()
    serializer_class = SKUSerializer
    pagination_class = MyPage

    def get_queryset(self):
        """搜索"""
        keyword = self.request.query_params.get('keyword')
        if keyword == '' or keyword is None:
            return SKU.objects.all()

        else:
            return SKU.objects.filter(name=keyword)

    def get(self, request):
        """查询"""
        sku_queryset = self.get_queryset()
        # 对数据集分页
        # 返回值: page指的是user_queryset分页的子集
        page = self.paginate_queryset(sku_queryset)
        # 判断
        if page:
            # 对这个子集进行序列化操作
            page_serializer = self.get_serializer(page, many=True)
            # 参数返回的是子集序列化后得到的数据
            return self.get_paginated_response(page_serializer.data)
        serializer = SKUSerializer(sku_queryset, many=True)
        return Response(serializer.data)

    def post(self, request):
        """添加"""
        sku_add = self.request.data
        serializer = self.get_serializer(data=sku_add)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.data, status=201)


# 请求方式： GET /meiduo_admin/skus/categories/
class GoodsGenericView(GenericAPIView):
    """查询三级联动"""
    queryset = GoodsCategory.objects.filter(parent_id__gt=37)
    serializer_class = SKUCategorieSerializer

    def get(self, request):
        classify = self.get_queryset()
        serializer = self.get_serializer(classify, many=True)
        return Response(serializer.data)


# 请求方式： GET /meiduo_admin/goods/simple/
class SPUSimpleView(GenericAPIView):
    queryset = SPU.objects.all()
    serializer_class = SPUSimpleSerializer

    def get(self, request):
        spu = self.get_queryset()
        serializer = self.get_serializer(spu, many=True)
        return Response(serializer.data)


# 请求方式： GET meiduo_admin/goods/(?P<pk>\d+)/specs/
class SPUSpecView(GenericAPIView):
    serializer_class = SPUSpecSerializer
    queryset = SPUSpecification.objects.all()

    def get(self, request, pk):
        spec = SPUSpecification.objects.filter(spu_id=pk)
        serializer = SPUSpecSerializer(spec, many=True)
        return Response(serializer.data)


# 请求方式： PUT /meiduo_admin/skus/(?P<pk>\d+)/  修改
class SKUGoodsView(GenericAPIView):
    serializer_class = SKUSerializer
    queryset = SKU.objects.all()

    def get(self, request, pk):
        sku_queryset = SKU.objects.get(pk=pk)
        serializer = SKUSerializer(sku_queryset)
        return Response(serializer.data)

    # 修改
    def put(self, request, pk):
        sku_obj = self.get_object()
        # data=request.data取到前段传来的对象, instance=sku_obj得到的数据
        serializer = SKUSerializer(instance=sku_obj, data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.data)

    def delete(self, request, pk):
        sku_obj = self.get_object()
        sku_good = sku_obj.delete()
        return Response(sku_good)